package com.mall.dubbo.api;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.mall.dubbo.mappers.SmsFlashPromotionMpper;
import com.mall.dubbo.mappers.SmsFlashPromotionProductRelationMapper;
import com.mall.dubbo.mappers.SmsFlashPromotionSessionMapper;
import com.tanhua.model.domain.SmsFlashPromotionProductRelation;
import com.tanhua.model.domain.SmsFlashPromotionSession;
import com.tanhua.model.vo.SmsFlashPromotionSessionDetail;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

@DubboService
public class SmsFlashPromotionSessionApiImpl implements SmsFlashPromotionSessionApi{
    @Autowired
    private SmsFlashPromotionSessionMapper smsFlashPromotionSessionMapper;


    @Autowired
    private SmsFlashPromotionProductRelationMapper smsFlashPromotionProductRelationMapper;

    @Override
    public List<SmsFlashPromotionSession> flashSessionList() {
        return smsFlashPromotionSessionMapper.selectList(new LambdaQueryWrapper<SmsFlashPromotionSession>());
    }

    /**
     * 修改秒杀时间段是否启用
     * @param id
     * @param starts
     */
    @Override
    public void updateStatus(Long id, Integer starts) {
        SmsFlashPromotionSession smsFlashPromotionSession = new SmsFlashPromotionSession();
        smsFlashPromotionSession.setId(id);
        smsFlashPromotionSession.setStatus(starts);
        smsFlashPromotionSessionMapper.updateById(smsFlashPromotionSession);
    }

    /**
     * 添加秒杀时间段
     * @param smsFlashPromotionSession
     * @return
     */
    @Override
    public void create(SmsFlashPromotionSession smsFlashPromotionSession) {
        smsFlashPromotionSessionMapper.insert(smsFlashPromotionSession);
    }

    /**
     * 修改秒杀时间段
     * @param smsFlashPromotionSession
     * @return
     */
    @Override
    public void update(SmsFlashPromotionSession smsFlashPromotionSession) {
        smsFlashPromotionSessionMapper.updateById(smsFlashPromotionSession);
    }


    /**
     * 删除秒杀时间
     * @param id
     * @return
     */
    @Override
    public void delete(Long id) {
        smsFlashPromotionSessionMapper.deleteById(id);
    }

    /**
     * 根据flashPromotionId 查询sms_flash_promotion_product_relation 得到 sms_flash_promotion_session 集合返回
     * @param flashPromotionId
     * @return
     */
    @Override
    public List<SmsFlashPromotionSessionDetail> findFlashPromotionIdList(Long flashPromotionId) {
        //查询sms_flash_promotion_session 得到所有的时间段
        List<SmsFlashPromotionSession> timeList = smsFlashPromotionSessionMapper.selectList(new LambdaQueryWrapper<SmsFlashPromotionSession>());

        List<SmsFlashPromotionSessionDetail> resultList = new ArrayList<>();

        //查询sms_flash_promotion_product_relation中 flashPromotionId和 SmsFlashPromotionSession.getID的数量
        for (SmsFlashPromotionSession smsFlashPromotionSession : timeList) {
            SmsFlashPromotionSessionDetail smsFlashPromotionSessionDetail = new SmsFlashPromotionSessionDetail();
            BeanUtils.copyProperties(smsFlashPromotionSession,smsFlashPromotionSessionDetail);

            LambdaQueryWrapper<SmsFlashPromotionProductRelation> lqw = new LambdaQueryWrapper<>();
            lqw.eq(smsFlashPromotionSession.getId() !=null,SmsFlashPromotionProductRelation::getFlashPromotionSessionId,smsFlashPromotionSession.getId())
                    .eq(flashPromotionId != null,SmsFlashPromotionProductRelation::getFlashPromotionId,flashPromotionId);

            smsFlashPromotionSessionDetail.setProductCount( smsFlashPromotionProductRelationMapper.selectCount(lqw));
            resultList.add(smsFlashPromotionSessionDetail);
        }

     return resultList;
    }
}
